#include "SockStream.h"
#include "SockAcceptor.h"

#include <errno.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <iostream>

int main ()
{
    InetAddr local_addr (9999);

    SockAcceptor acceptor;
    if (acceptor.open (local_addr) != 0)
    {
        std::cout << errno << std::endl;
        perror ("open");
        return 0;
    }
    char buff[512] = {0};
    int ep_fd = epoll_create (200);
    struct epoll_event ep, evs[20];
    ep.events  = EPOLLIN | EPOLLHUP | EPOLLERR;
    ep.data.fd = acceptor.handle ();
    NDK::set_non_block_mode(acceptor.handle ());
    epoll_ctl (ep_fd, EPOLL_CTL_ADD, acceptor.handle (), &ep);

    while(1)
    {
        int ret = epoll_wait (ep_fd, evs, 20, -1);
        if (ret <= 0)
        {
            perror ("select");
            std::cout << "timeout of error, ret = " << ret << std::endl;
        }
        SockStream peer;
        InetAddr remote_addr;
        if (acceptor.accept (peer, &remote_addr) != 0)
        {
            perror ("accept");
            //return 0;
        }
        std::cout << "accept a connection " << std::endl;
    }
    return 0;
}
